package com.example.restatr.Atcivity.DigitalLibrary;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class DigitalLibraryAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private RecyclerView lv;
    private TextView bottomText;
    private List<DigitalLibraryBean.RowsBean> rowsBeans = new ArrayList<>();
    private int pagerNum = 1;
    private int total;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_digital_library_at );

        toolbar = findViewById( R.id.toolbar );
        barTitle = findViewById( R.id.bar_Title );
        search = findViewById( R.id.search );
        lv = findViewById( R.id.lv );
        bottomText = findViewById( R.id.bottomText );

        Tools.SetBarTitle( toolbar, barTitle, "数字图书馆", search, this, false );
        GetLibrary();

        lv.addOnScrollListener( new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged( recyclerView, newState );
                if (!recyclerView.canScrollVertically( 1 ) && newState == 0) {
                    if (rowsBeans.size() < total) {
                        pagerNum++;
                        GetLibrary();
                    } else {
                        bottomText.setText( "——到底了——" );
                    }
                }
            }
        } );
    }

    //获取图书馆列表
    private void GetLibrary() {
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/digital-library/library/list?pageSize=10&pageNum=" + pagerNum, MainActivity.sp.getString( "token", "" ) );
            DigitalLibraryBean digitalLibraryBean = new Gson().fromJson( token, DigitalLibraryBean.class );
            total = digitalLibraryBean.getTotal();
            if (digitalLibraryBean.getCode() == 200) {
                rowsBeans.addAll( digitalLibraryBean.getRows() );
                runOnUiThread( () -> {
                    lv.setAdapter( new adapter() );
                } );
            }
        } ).start();
    }

    //获取图书馆详情
    private void GetLibraryInfo(int id, String state) {
        ProgressDialog 加载中 = ProgressDialog.show( DigitalLibraryAt.this, "", "加载中" );
        new Thread( () -> {
            String token = Tools.Get( "/prod-api/api/digital-library/library/" + id, MainActivity.sp.getString( "token", "" ) );
            String commentTotal = Tools.Get( "/prod-api/api/digital-library/library-comment/list?libraryId=" + id, MainActivity.sp.getString( "token", "" ) );
            LibraryCommentBean libraryCommentBean = new Gson().fromJson( commentTotal, LibraryCommentBean.class );
            DigitalLibraryBean rowsBean = new Gson().fromJson( token, DigitalLibraryBean.class );
            runOnUiThread( () -> {
                Intent intent = new Intent( DigitalLibraryAt.this, LibraryInfoAt.class );
                intent.putExtra( "data", new Gson().toJson( rowsBean.getData() ) );
                intent.putExtra( "total", libraryCommentBean.getData().size() );
                intent.putExtra( "state", state );
                加载中.dismiss();
                startActivity( intent );
            } );
        } ).start();
    }


    class adapter extends RecyclerView.Adapter<adapter.adapterViewHolder> {

        @NonNull
        @Override
        public adapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = getLayoutInflater().inflate( R.layout.zfz_list_item, parent, false );
            return new adapterViewHolder( inflate );
        }

        @Override
        public void onBindViewHolder(@NonNull adapterViewHolder holder, int position) {
            Glide.with( DigitalLibraryAt.this )
                    .load( Tools.ip + rowsBeans.get( position ).getImgUrl() )
                    .transform( new RoundedCorners( 15 ) )
                    .into( holder.tupian );
            holder.name.setText( rowsBeans.get( position ).getName() );
            holder.diz.setText( rowsBeans.get( position ).getAddress() );
            holder.phone.setText( rowsBeans.get( position ).getBusinessHours() );
            if (rowsBeans.get( position ).getBusinessState().equals( "0" )) {
                holder.mj.setText( "已闭店" );
            } else {
                holder.mj.setText( "营业中" );
            }
            holder.jg.setVisibility( View.GONE );
            holder.itemView.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GetLibraryInfo( rowsBeans.get( position ).getId(), holder.mj.getText().toString() );

                }
            } );
        }

        @Override
        public int getItemCount() {
            return rowsBeans.size();
        }

        class adapterViewHolder extends RecyclerView.ViewHolder {
            private ImageView tupian;
            private TextView name;
            private TextView diz;
            private TextView phone;
            private TextView mj;
            private TextView hux;
            private TextView jg;
            private CardView cardBtn;

            public adapterViewHolder(@NonNull View itemView) {
                super( itemView );
                tupian = itemView.findViewById( R.id.tupian );
                name = itemView.findViewById( R.id.name );
                diz = itemView.findViewById( R.id.diz );
                phone = itemView.findViewById( R.id.phone );
                mj = itemView.findViewById( R.id.mj );
                hux = itemView.findViewById( R.id.hux );
                jg = itemView.findViewById( R.id.jg );
                cardBtn = itemView.findViewById( R.id.cardBtn );
            }
        }
    }

}